package com.ruoyi.web.controller.transaction;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.transaction.domain.Contact;
import com.ruoyi.transaction.domain.Order;
import com.ruoyi.transaction.domain.OrderItemTmp;
import com.ruoyi.transaction.service.IOrderItemTmpService;
import com.ruoyi.transaction.service.IOrderService;
import com.ruoyi.transaction.service.impl.ContactService;
import com.ruoyi.transaction.vo.OrderItemTmpVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by gzdmt01 on 2019/3/28.
 */
@Api(tags="运单管理")
@Controller
@RequestMapping("/transaction/order")
public class orderController extends BaseController {
    private String prefix = "/transaction/order";

    @Autowired
    private IOrderItemTmpService orderItemTmpService;

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private ContactService contactService;

    @ApiOperation("跳转至临时订单明细页面")
    @GetMapping()
    public  String  orderItem(){
        return prefix+"/orderItemTmp";
    }

    @ApiOperation("跳转至添加临时订单明细页面")
    @GetMapping("/addOrderItemTmp")
    public String addOrderItemTmp(){
        return prefix+"/addOrderItemTmp";
    }

    @Log(title = "新增包裹",businessType = BusinessType.INSERT)
    @ApiOperation("新增包裹")
    @ApiImplicitParam(name = "orderItemTmp", value = "单个包裹信息", dataType = "OrderItemTmp")
    @PostMapping("/addOrderItemTmp")
    @ResponseBody
    public AjaxResult addOrederItemTmp(@RequestBody  List<OrderItemTmp> orderItemTmpList){
        Integer userId= Integer.valueOf(""+ShiroUtils.getUserId());
        return orderItemTmpService.batchInsert(orderItemTmpList,userId);
    }


    @ApiOperation("包裹列表")
    @PostMapping("/listOrderItemTmp")
    @ResponseBody
    public TableDataInfo listOrederItemTmp( OrderItemTmp orderItemTmp){
        System.out.println(orderItemTmp);
       List<OrderItemTmpVo> list= orderItemTmpService.listOrederItemTmp(ShiroUtils.getUserId(),orderItemTmp);
        return getDataTable(list);
    }

    @ApiOperation("跳转至临时订单明细修改页面")
    @GetMapping("/editOrderItemTmp/{id}")
    public String editOrderItemTmp(@PathVariable("id") Integer OrderItemTmpId, Model model){
        OrderItemTmp orderItemTmp =orderItemTmpService.selectById(OrderItemTmpId);
        model.addAttribute("orderItemTmp",orderItemTmp);
        return prefix+"/editOrderItemTmp";
    }

    @Log(title = "包裹修改")
    @ApiOperation("临时订单明细修改")
    @PostMapping("/editOrderItemTmp")
    @ResponseBody
    public AjaxResult editSaveOrderItemTmp(OrderItemTmp orderItemTmp){
        int count=orderItemTmpService.editSaveOrderItemTmp(orderItemTmp);
        return toAjax(count);
    }

    @Log(title = "包裹删除")
    @ApiOperation("临时订单明细删除")
    @PostMapping("/removeOrderItemTmp")
    @ResponseBody
    public AjaxResult removeOrderItemTmp(@RequestParam(value = "ids") String itemIds){
        System.out.println("==="+itemIds);
        Long userId = ShiroUtils.getUserId();
        return orderItemTmpService.deleteByIds(itemIds,userId);
    }

    @Log(title = "跳转至添加订单页面")
    @ApiOperation("跳转至添加订单页面")
    @GetMapping("/addOrder")
    public String addOrderPage( ModelMap map){
        Long userId = ShiroUtils.getUserId();
        List<OrderItemTmpVo> list= orderItemTmpService.listOrederItemTmp(ShiroUtils.getUserId(),null);
        Contact contact = contactService.selectDefaultContact(Integer.parseInt(userId+""));
        map.put("orderItemList",list);
        map.put("contact",contact);
        return prefix+"/addOrder";
    }

    @Log(title = "添加订单")
    @PostMapping("/addOrder")
    @ApiOperation("添加订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单信息", dataType = "Order"),
            @ApiImplicitParam(name = "orderItemTmpIds", value = "订单明细ID信息", dataType = "string",paramType = "query")})
    public AjaxResult addOrder(@RequestBody Order order, String orderItemTmpIds){

        Long userId = ShiroUtils.getUserId();
        return iOrderService.add(order,orderItemTmpIds,userId);

    }
}
